package com.example.number1.ui.theme.util;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.number1.R;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_MESSAGE = 1;
    private static final int VIEW_TYPE_TIME = 2;
    private static final int TIMER_DELAY = 10;

    private List<Object> items;
    private Timer timer;
    private TimerTask timerTask;
    private OnTimerTickListener onTimerTickListener;

    public ChatAdapter(List<Object> items) {
        this.items = items;
        timer = new Timer();
    }

    // 设置回调接口
    public void setOnTimerTickListener(OnTimerTickListener listener) {
        this.onTimerTickListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        Object item = items.get(position);
        if (item instanceof Message) {
            return VIEW_TYPE_MESSAGE;
        } else if (item instanceof String) {
            return VIEW_TYPE_TIME;
        }
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        if (viewType == VIEW_TYPE_MESSAGE) {
            // 消息项的布局
            View itemView = inflater.inflate(R.layout.item_message, parent, false);
            return new MessageViewHolder(itemView);
        } else if (viewType == VIEW_TYPE_TIME) {
            // 时间项的布局
            View timeView = inflater.inflate(R.layout.item_time, parent, false);
            return new TimeViewHolder(timeView);
        }

        throw new IllegalArgumentException("Invalid view type");
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object item = items.get(position);

        if (holder instanceof MessageViewHolder && item instanceof Message) {
            // 绑定消息项数据
            Message message = (Message) item;
            ((MessageViewHolder) holder).bind(message);
        } else if (holder instanceof TimeViewHolder && item instanceof String) {
            // 绑定时间项数据
            String time = (String) item;
            ((TimeViewHolder) holder).bind(time);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    // 执行定时器任务
    private void startTimer(final Runnable task) {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                task.run();
            }
        };
        timer.schedule(timerTask, TIMER_DELAY * 1000);
    }

    // 取消定时器任务
    private void cancelTimer() {
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }

    // 定时器回调接口
    public interface OnTimerTickListener {
        void onTick();
    }

    // 消息项的 ViewHolder
    private static class MessageViewHolder extends RecyclerView.ViewHolder {
        private TextView messageText;

        public MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            messageText = itemView.findViewById(R.id.content_text_view);
        }

        public void bind(Message message) {
           // messageText.setText(message.getText());
        }
    }

    // 时间项的 ViewHolder
    private static class TimeViewHolder extends RecyclerView.ViewHolder {
        private TextView timeText;

        public TimeViewHolder(@NonNull View itemView) {
            super(itemView);
            timeText = itemView.findViewById(R.id.time_text);
        }

        public void bind(String time) {
            timeText.setText(time);
        }
    }
}
